# -*- coding:utf-8 -*-

# @Time    : 2023/9/20 21:07
# @Author  : yaodong
# @Email   : yaodong@lingxi.ai
# @Software: LLM_internal

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


import asyncio
import copy
import json
import math
import urllib

from common.log import logger
from db.redis.redis_service import RedisMemory
from urllib.parse import urlparse, parse_qs

import aiohttp


def extract_ids_from_url(url):
    try:
        decoded_url = urllib.parse.unquote(url)
        parsed_url = urlparse(decoded_url)

        # 获取查询参数
        query_params = parse_qs(parsed_url.query)

        # 获取prodId和planId参数值
        if 'restrictGeneParams' in query_params:
            restrictGeneParams = query_params.get("restrictGeneParams", [''])[0]
            restrictGeneParams = json.loads(restrictGeneParams)
            genes = restrictGeneParams.get("genes")
            return genes
        else:
            return None
    except Exception as e:
        print("提取参数时发生错误:", str(e))
        return None


def change_json_arr2obj(original_json):
    # 初始化一个空字典来存储提取后的键值对
    result_dict = {}

    # 遍历原始JSON数组，提取键值对并添加到新字典中
    for item in original_json:
        key = item["key"]
        value = item["value"]
        result_dict[key] = value
    result_json = json.dumps(result_dict, indent=4, ensure_ascii=False)
    return result_json


def store_redis_product_cn(url, insurance_name):
    genes = extract_ids_from_url(url)
    if genes:
        param = change_json_arr2obj(genes)
    else:
        param = ''

    product_info = {
        "insurance_API": url.split("&restrictGeneParams")[0][:],
        "insurance_params": param.replace('\n', '').replace(' ', '')
    }
    memory_cache.set_memory("insurance_product_cn", insurance_name, product_info)


if __name__ == '__main__':
    memory_cache = RedisMemory()
    # 添加测算保费url
    url = """
    https://www.huize.com/apps/cps/product/tryTrial?callback=jQuery182027272371233650694_1709284859563&uid=10000000&prodId=102933&planId=106591&restrictGeneParams=%7B%22productId%22%3A%22102933%22%2C%22productPlanId%22%3A%22106591%22%2C%22genes%22%3A%5B%7B%22sort%22%3A7%2C%22protectItemId%22%3A15629%2C%22key%22%3A%22%22%2C%22value%22%3A%2210%E4%B8%87%E5%85%83%22%7D%2C%7B%22sort%22%3A1%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22vesterAge%22%2C%22value%22%3A%222005-10-06%22%7D%2C%7B%22sort%22%3A2%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22vesterSex%22%2C%22value%22%3A%22%E7%94%B7%22%7D%2C%7B%22sort%22%3A3%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22insureForSelf%22%2C%22value%22%3A%22%E5%90%A6%22%7D%2C%7B%22sort%22%3A4%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22insurantDate%22%2C%22value%22%3A%222022-10-22%22%7D%2C%7B%22sort%22%3A5%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22sex%22%2C%22value%22%3A%22%E7%94%B7%22%7D%2C%7B%22sort%22%3A6%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22insurantJob%22%2C%22value%22%3A%221-4%E7%B1%BB%22%7D%2C%7B%22sort%22%3A8%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22insurantDateLimit%22%2C%22value%22%3A%22%E7%BB%88%E8%BA%AB%22%7D%2C%7B%22sort%22%3A9%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22paymentType%22%2C%22value%22%3A%22%E5%B9%B4%E4%BA%A4%22%7D%2C%7B%22sort%22%3A10%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22insureAgeLimit%22%2C%22value%22%3A%2235%E5%B9%B4%22%7D%2C%7B%22sort%22%3A11%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22isInsureFive%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A12%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22isInsureFour%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A13%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22isInsureTwo%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A14%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22isInsureThree%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A15%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22isInsure%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A16%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22policyholderExemption%22%2C%22value%22%3A%22%E4%B8%8D%E5%90%AB%22%7D%2C%7B%22sort%22%3A17%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22additionalRiskPeriod%22%2C%22value%22%3A%22%E7%BB%88%E8%BA%AB%22%7D%2C%7B%22sort%22%3A18%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22paymentType1%22%2C%22value%22%3A%22%E5%B9%B4%E4%BA%A4%22%7D%2C%7B%22sort%22%3A19%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22policyholdeExemptionAdditionalInsureAgeLimit%22%2C%22value%22%3A%22%E4%B8%8D%E6%8A%95%E4%BF%9D%22%7D%5D%2C%22optGeneOldValue%22%3A%7B%22sort%22%3A2%2C%22protectItemId%22%3A%22%22%2C%22key%22%3A%22vesterSex%22%2C%22value%22%3A%22%E5%A5%B3%22%7D%7D&_=1709284873870
    
     """
    url = url.replace(" ", "")
    insurance_name = "守卫者6号重大疾病保险-保终身（A款）"
    store_redis_product_cn(url, insurance_name)

    # 保险产品项对应的字典
    key_mapping = {
    }
    memory_cache.set_keys_in_redis(insurance_name, key_mapping)

    memory_cache.get_memory("insurance_product_cn", insurance_name)
